{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 07, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2>Superdense Coding</h2>\n",
    "\n",
    "Asja has a qubit, initially set to $ \\ket{0} $.\n",
    "\n",
    "Balvis has a qubit, initially set to $ \\ket{0} $."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Entanglement </h3>\n",
    "    \n",
    "Asja applies Hadamard operator to her qubit. \n",
    "\n",
    "The quantum state of Asja's qubit is $ \\vhadamardzero $.\n",
    "\n",
    "Then, Asja and Balvis combine their qubits. Their quantum state is\n",
    "\n",
    "$ \\vhadamardzero \\otimes \\vzero = \\myvector{ \\frac{1}{\\sqrt{2}} \\\\ 0 \\\\ \\frac{1}{\\sqrt{2}} \\\\ 0 }  $.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Asja and Balvis apply CNOT operator on two qubits.\n",
    "\n",
    "The new quantum state is\n",
    "\n",
    "$ \\CNOT \\myvector{ \\frac{1}{\\sqrt{2}} \\\\ 0 \\\\ \\frac{1}{\\sqrt{2}} \\\\ 0 }  = \\myvector{ \\frac{1}{\\sqrt{2}} \\\\ 0 \\\\0 \\\\ \\frac{1}{\\sqrt{2}} } = \\frac{1}{\\sqrt{2}}\\ket{00} + \\frac{1}{\\sqrt{2}}\\ket{11}  $.\n",
    "\n",
    "At this moment, Asja's and Balvis' qubits are correlated to each other.\n",
    "\n",
    "If we measure both qubits, we can observe either state $ \\ket{00} $ or state $ \\ket{11} $. \n",
    "\n",
    "Suppose that Asja observes her qubit secretly. \n",
    "<ul>\n",
    "    <li> When Asja sees the result $ \\ket{0} $, then Balvis' qubit also collapses to state $ \\ket{0} $. Balvis cannot observe state $ \\ket{1} $. </li>\n",
    "    <li> When Asja sees the result $ \\ket{1} $, then Balvis' qubit also collapses to state $ \\ket{1} $. Balvis cannot observe state $ \\ket{0} $. </li>\n",
    "</ul>\n",
    "    \n",
    "Experimental results have confirmed that this happens even if there is a physical distance between Asja's and Balvis' qubits. \n",
    "\n",
    "It seems correlated quantum particales can affect each other instantly, even if they are in the different part of the universe. \n",
    "\n",
    "If two qubits are correlated in this way, then we say that they are <b>entangled</b>.\n",
    "\n",
    "<i> <u>Technical note</u>: \n",
    "    \n",
    "If the quantum state of two qubits can be written as $ \\ket{u} \\otimes \\ket{v} $, then two qubits are not correlated, where $ \\ket{u} $ and $ \\ket{v} $ are the quantum states of the first and second qubits.\n",
    "\n",
    "On the other hand, if the quantum state of two qubits cannot be written as $ \\ket{u} \\otimes \\ket{v} $, then there is an entanglement between the qubits.\n",
    "</i>\n",
    "\n",
    "<b> Entangled qubits can be useful </b>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> The quantum commmunication </h3>\n",
    "\n",
    "After having the entanglement, Balvis takes his qubit and goes away.\n",
    "\n",
    "Asja will send two bits of information by only sending her qubit.\n",
    "\n",
    "Now, we describe this protocol."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Asja has two bits of classical information: $ a,b \\in \\{0,1\\} $. \n",
    "\n",
    "There are four possible values for the pair $ (a,b) $:  $ (0,0), (0,1), (1,0),\\mbox{ or } (1,1) $. \n",
    "\n",
    "If $a$ is 1, then Asja applies z-gate, i.e., $ Z = \\Z $, to her qubit.\n",
    "\n",
    "If $b$ is 1, then Asja applies x-gate (NOT operator) to her qubit.\n",
    "\n",
    "Then, Asja sends her qubit to Balvis."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> After the communication </h3>\n",
    "\n",
    "Balvis has both qubits.\n",
    "\n",
    "Balvis applies cx-gate (CNOT operator), where Asja's qubit is the controller.\n",
    "\n",
    "Then, Balvis applies h-gate (Hadamard operator) to Asja's qubit.\n",
    "\n",
    "Balvis measures both qubits. \n",
    "\n",
    "The measurement result will be exactly $ (a,b) $."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 1</h3>\n",
    "\n",
    "Verify the correctness of the above protocol.\n",
    "\n",
    "For each pair of $ (a,b) \\in \\left\\{ (0,0), (0,1), (1,0),(1,1) \\right\\} $:\n",
    "<ul>\n",
    "    <li> Create a quantum curcuit with two qubits: Asja's and Balvis' qubits.</li>\n",
    "    <li> Both are initially set to $ \\ket{0} $.</li>\n",
    "    <li> Apply h-gate (Hadamard) to the first qubit. </li>\n",
    "    <li> Apply cx-gate (CNOT) with parameters first-qubit and second-qubit. </li>\n",
    "</ul>\n",
    "\n",
    "They are separated now.\n",
    "\n",
    "<ul>\n",
    "    <li> If $ a $ is 1, then apply z-gate to the first qubit. </li>\n",
    "    <li> If $ b $ is 1, then apply x-gate (NOT) to the first qubit. </li>\n",
    "</ul>\n",
    "\n",
    "Asja sends her qubit to Balvis.\n",
    "\n",
    "<ul>\n",
    "    <li> Apply cx-gate (CNOT) with parameters first-qubit and second-qubit.</li>\n",
    "    <li> Apply h-gate (Hadamard) to the first qubit. </li>\n",
    "    <li> Measure both qubits, and compare the results with pair $ (a,b) $. </li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import all necessary objects and methods for quantum circuits\n",
    "from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer\n",
    "from qiskit.tools.visualization import matplotlib_circuit_drawer as drawer\n",
    "\n",
    "all_pairs = ['00','01','10','11']\n",
    "\n",
    "for pair in all_pairs:\n",
    "    #\n",
    "    # your code is here\n",
    "    #\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B60_Superdense_Coding_Solution.ipynb#task1\">click for our solution</a>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
